package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorService;
import com.heima.common.constants.ApUserFollowConstans;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;

import com.heima.utils.thread.ApUserThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApBehaviorServiceImpl implements ApBehaviorService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        try {
            String key = ApUserFollowConstans.Article_Like_key + dto.getArticleId();
            String hashKey = ApUserFollowConstans.Like_Fowllow_key
                    + ApUserThreadUtil.getUser().getId();
            if(dto.getOperation()==0){
                redisTemplate.opsForHash().put(key,hashKey,"1");
            }else {
                redisTemplate.opsForHash().delete(key, hashKey);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(505, "操作失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        try {
            String key = ApUserFollowConstans.Article_Dislike_key + dto.getArticleId();
            String hashKey = ApUserFollowConstans.Dislike_Fowllow_key
                    + ApUserThreadUtil.getUser().getId();
            if (dto.getType() == 0) {
                redisTemplate.opsForHash().put(key, hashKey, "1");
            } else {
                redisTemplate.opsForHash().delete(key, hashKey);
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(504, "操作失败");
        }
    }

    @Override
    public ResponseResult read(ReadDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        String key = ApUserFollowConstans.Read_Count_Key + dto.getArticleId();
        Boolean aBoolean = stringRedisTemplate.hasKey(key);

            stringRedisTemplate.boundValueOps(key).increment(dto.getCount());
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}
